// axios封装
import axios from "axios";

const services = axios.create({
  baseURL:'',
  timeout:10 * 1000,
  headers:{
    "Content-Type":"application/json;charset=UTF-8"
  },
  transformRequest:[
    data => JSON.stringify(data) || ''
  ]
})

services.interceptors.request.use(config => {
  return config
}, err => {
  return Promise.reject(err)
})

services.interceptors.response.use(
  (response) => {
    return response
  }, 
  (err) => {
    if(err.response.status === 401) {
      window.location.reload()
      return Promise.reject('未认证')
    }
    if(err.response.status === 403) {
      return Promise.reject('无权限')
    }
    if(err.response.status >= 500) {
      return Promise.reject('服务器发生错误')
    }
    return Promise.reject(err)
  }
)

export default services